package com.bulletelc.apollopre.module.main.home;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.ProductDetail;
import com.bulletelc.apollopre.interfaces.listener.OnItemClickListener;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;

import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/12
 * desc  : 套餐选购区的adapter
 */

public class PackageAdapter extends RecyclerView.Adapter<PackageAdapter.MyViewHolder> {
    private Context context;
    private List<ProductDetail> productDetailList;
    private LayoutInflater inflater;
    private OnItemClickListener listener;

    public PackageAdapter(Context context, List<ProductDetail> productDetailList) {
        this.context = context;
        this.productDetailList = productDetailList;
        inflater = LayoutInflater.from(context);
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.package_list_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        ProductDetail productDetail = productDetailList.get(position);
        holder.tvProductName.setText(productDetail.getName());
        holder.tvProductPrice.setText("¥" + productDetail.getLowest_price());
        holder.tvSold.setText("已售:" + productDetail.getSold());
        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holder.ivProductImg, productDetail.getOriginalUrl()));
        holder.llItemPackage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemClick(holder.llItemPackage, pos);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return productDetailList != null ? productDetailList.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llItemPackage;
        private ImageView ivProductImg;
        private TextView tvProductName;
        private TextView tvProductPrice;
        private TextView tvSold;

        public MyViewHolder(View itemView) {
            super(itemView);
            llItemPackage = itemView.findViewById(R.id.llItemPackage);
            ivProductImg = itemView.findViewById(R.id.ivProductImg);
            tvProductName = itemView.findViewById(R.id.tvProductName);
            tvProductPrice = itemView.findViewById(R.id.tvProductPrice);
            tvSold = itemView.findViewById(R.id.tvSold);
        }
    }

}
